/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking;

import easybooking.clients.gui.EBInterface;
import java.awt.Component;
import java.awt.Container;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;

/**
 *
 * @author Tim Schroeder
 */
public class FlightServerGui {

    private JFrame serverMainFrame;
    private JMenuBar mainMenuBar;
    private JMenu helpJMenu;
    private JMenu fileJMenu;
    private JMenuItem help;
    private JMenuItem about;
    private JMenuItem exit;
    private JPanel mainPanel;
    private JPanel buttonPanel;
    private JButton btnStartServer;
    private JButton btnStopServer;
    private FlightServerController flightServerController;
    private JPanel configPanel;
    private JLabel lblRmiHost;
    private JTextField txfRmiHost = new JTextField(20);
    private JLabel lblMySqlHost;
    private JTextField txfMySqlHost = new JTextField(20);
    private JLabel lblDbUser;
    private JTextField txfDbUser = new JTextField(20);
    private JLabel lblDbPass;
    private JPasswordField pwfDbPass = new JPasswordField(20);
    private JLabel lblDbName;
    private JTextField txfDbName = new JTextField(20);
    private JLabel lblServerLog;
    private JTextArea txaServerLog;
    private JPanel contentPanel;
    private JButton btnExitServer;

    public JTextArea getTextArea() {
        return txaServerLog;
    }

    public void createAndShowGUI(FlightServerController fsc) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }
        this.flightServerController = fsc;
        this.createFrame();
    }

    public void setTextFields(String rmiHost, String dbHost, String dbName, String dbUser, String dbPassword) {
        txfRmiHost.setText(rmiHost);
        txfMySqlHost.setText(dbHost);
        txfDbName.setText(dbName);
        txfDbUser.setText(dbUser);
        pwfDbPass.setText(dbPassword);
    }

    public void createFrame() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }

        serverMainFrame = new JFrame("EasyBooking Server");
        serverMainFrame.setDefaultLookAndFeelDecorated(true);
        serverMainFrame.setSize(new Dimension(500, 400));
        serverMainFrame.setJMenuBar(this.createMenuBar());
        serverMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        serverMainFrame.setContentPane(this.createContentPanel());
        serverMainFrame.setResizable(false);
        serverMainFrame.setLocationRelativeTo(null);
        serverMainFrame.pack();
        serverMainFrame.setVisible(true);




    }

    private JMenuBar createMenuBar() {

        mainMenuBar = new JMenuBar();
        fileJMenu = new JMenu("File");
        helpJMenu = new JMenu("Help");

        help = new JMenuItem("Help");
        about = new JMenuItem("About");
        exit = new JMenuItem("Exit");

        exit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);

            }
        });

        about.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                JOptionPane.showMessageDialog(null, "(c)");
            }
        });

        help.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                java.net.URL helpURL = EBInterface.class.getResource("../resources/manual.pdf");
                try {
                    Desktop.getDesktop().open(new File(helpURL.toURI()));
                } catch (URISyntaxException ex) {
                    Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });


        helpJMenu.add(help);
        helpJMenu.add(about);
        fileJMenu.add(exit);
        mainMenuBar.add(fileJMenu);
        mainMenuBar.add(helpJMenu);
        return mainMenuBar;
    }

    public JPanel createContentPanel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }
        contentPanel = new JPanel();

        // Sets the Layout of the contentPanel
        contentPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        // Places the GUI elements in the layout using addComponent                                                         x, y, w, h, wx, wy
        addComponent(contentPanel, gbl, this.createMainPanel(), GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 0, 0);
        addComponent(contentPanel, gbl, this.createButtonPanel(), GridBagConstraints.NONE, GridBagConstraints.WEST, 0, 1, 1, 1, 0, 0);

        return contentPanel;

    }

    private JPanel createMainPanel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }

        configPanel = new JPanel();

        // Sets the Border
        configPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Easybooking Database Configuration"),
                BorderFactory.createEmptyBorder(20, 20, 20, 20)));


        configPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        // Places the GUI elements in the layout using addComponent                                          x, y, w, h, wx, wy
//        lblRmiHost = new JLabel("RMI-Host:");
//        addComponent(configPanel, gbl, lblRmiHost, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 0, 0);
//
//        addComponent(configPanel, gbl, txfRmiHost, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 0, 1, 1, 0, 0);

        lblMySqlHost = new JLabel("MySQL-Host:");
        addComponent(configPanel, gbl, lblMySqlHost, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 1, 1, 1, 0, 0);

        addComponent(configPanel, gbl, txfMySqlHost, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 1, 1, 1, 0, 0);

        lblDbName = new JLabel("DB-Name:");
        addComponent(configPanel, gbl, lblDbName, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 2, 1, 1, 0, 0);

        addComponent(configPanel, gbl, txfDbName, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 2, 1, 1, 0, 0);

        lblDbUser = new JLabel("DB-User:");
        addComponent(configPanel, gbl, lblDbUser, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 3, 1, 1, 0, 0);

        addComponent(configPanel, gbl, txfDbUser, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 3, 1, 1, 0, 0);

        lblDbPass = new JLabel("DB-Password:");
        addComponent(configPanel, gbl, lblDbPass, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 4, 1, 1, 0, 0);

        addComponent(configPanel, gbl, pwfDbPass, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 4, 1, 1, 0, 0);

        lblServerLog = new JLabel("Server-Log:");
        addComponent(configPanel, gbl, lblServerLog, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 5, 1, 1, 0, 0);

        txaServerLog = new JTextArea(10, 40);
        JScrollPane sp = new JScrollPane(txaServerLog);
        sp.setHorizontalScrollBarPolicy(sp.HORIZONTAL_SCROLLBAR_NEVER);
        addComponent(configPanel, gbl, sp, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 6, 2, 1, 0, 0);

        return configPanel;
//
//        Places the GUI elements in the layout using addComponent                                           x, y, w, h, wx, wy
//
//       addComponent(mainPanel, gbl, this.createButtonPanel(), GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 0, 0);
    }

    private JPanel createButtonPanel() {

        buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        btnStartServer = new JButton("Start Server");
        btnStartServer.setActionCommand("startServer");
        btnStartServer.addActionListener(flightServerController);

        btnStopServer = new JButton("Stop Server");
        btnStopServer.setActionCommand("stopServer");
        btnStopServer.addActionListener(flightServerController);

        btnExitServer = new JButton("Exit");
        btnExitServer.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);

            }
        });

        // Sets the buttons the same size
        btnStopServer.setPreferredSize(btnStartServer.getPreferredSize());
        btnExitServer.setPreferredSize(btnStopServer.getPreferredSize());

        // Adds the buttons to the buttonPanel
        addComponent(buttonPanel, gbl, btnStartServer, GridBagConstraints.NONE, GridBagConstraints.WEST, 0, 0, 1, 1, 0, 0);
        addComponent(buttonPanel, gbl, btnStopServer, GridBagConstraints.NONE, GridBagConstraints.WEST, 1, 0, 1, 1, 1, 0);
        addComponent(buttonPanel, gbl, btnExitServer, GridBagConstraints.NONE, GridBagConstraints.EAST, 2, 0, 1, 1, 1, 0);

        return buttonPanel;
    }

    public String getRmiHost() {
        return txfRmiHost.getText();
    }

    public String getDbHost() {
        return txfMySqlHost.getText();
    }

    public String getDbName() {
        return txfDbName.getText();
    }

    public String getDbUser() {
        return txfDbUser.getText();
    }

    public String getDbPassword() {
        return pwfDbPass.getText();
    }

    static void addComponent(Container cont, GridBagLayout gbl, Component c, int fill, int anchor, int x, int y, int width, int hight, double weightx, double weighty) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = fill;
        gbc.anchor = anchor;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = hight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.insets = new Insets(10, 10, 10, 10);
        //gbl.setConstraints(c, gbc);
        cont.add(c, gbc);


    }
}



